//
//  NSString+BCPNSString.m
//  BCP
//
//  Created by zhangke on 16/3/24.
//  Copyright © 2016年 525happy. All rights reserved.
//

#import "NSString+BCPNSString.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>

@implementation NSString (BCPNSString)

- (NSString *)hmacSha1:(NSString*)key
{
    const char *cKey  = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [self cStringUsingEncoding:NSUTF8StringEncoding];
    unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    //    NSLog(@"%s",cHMAC);

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    NSString *signStr = [HMAC base64EncodedStringWithOptions:0];
    //    NSLog(@"signStr:%@",signStr);
    return signStr;
}

- (BOOL)stringContainsEmoji
{
    //    __block BOOL returnValue = NO;
    //    [self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    //
    //        const unichar hs = [substring characterAtIndex:0];
    //        // surrogate pair
    //        if (0xd800 <= hs && hs <= 0xdbff) {
    //            if (substring.length > 1) {
    //                const unichar ls = [substring characterAtIndex:1];
    //                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
    //                if (0x1d000 <= uc && uc <= 0x1f77f) {
    //                    returnValue = YES;
    //                }
    //            }
    //        } else if (substring.length > 1) {
    //            const unichar ls = [substring characterAtIndex:1];
    //            if (ls == 0x20e3) {
    //                returnValue = YES;
    //            }
    //        } else {
    //            // non surrogate
    //            if (0x2100 <= hs && hs <= 0x27ff) {
    //                returnValue = YES;
    //            } else if (0x2B05 <= hs && hs <= 0x2b07) {
    //                returnValue = YES;
    //            } else if (0x2934 <= hs && hs <= 0x2935) {
    //                returnValue = YES;
    //            } else if (0x3297 <= hs && hs <= 0x3299) {
    //                returnValue = YES;
    //            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
    //                returnValue = YES;
    //            }
    //        }
    //    }];
    //    return returnValue;

    //textfield 原生九宫格中文输入乱码
    NSArray *arr = [NSArray arrayWithObjects:@"➋",@"➌",@"➍",@"➎",@"➏",@"➐",@"➑",@"➒",@"☻", nil];
    for (int i=0; i<[arr count]; i++) {
        if ([self isEqualToString:arr[i]]) {
            return NO;
        }
    }

    __block BOOL returnValue = NO;
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

        const unichar hs = [substring characterAtIndex:0];
        //        NSLog(@"hs:%hu",hs);

        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                if (0xdc00 <= ls && ls <= 0xe290) {
                    returnValue = YES;
                }
            }
        }
        else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff) {
                returnValue = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                returnValue = YES;
            }
        }
    }];
    return returnValue;
}

+ (BOOL)stringContainsEmoji:(NSString *)string
{
    NSArray *arr = [NSArray arrayWithObjects:@"➋",@"➌",@"➍",@"➎",@"➏",@"➐",@"➑",@"➒",@"☻", nil];
    for (int i=0; i<[arr count]; i++) {
        if ([string isEqualToString:arr[i]]) {
            return NO;
        }
    }
    // 过滤所有表情。returnValue为NO表示不含有表情，YES表示含有表情
    __block BOOL returnValue = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

        const unichar hs = [substring characterAtIndex:0];
        //        NSLog(@"hs:%hu",hs);

        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                //                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                //                if (0x1d000 <= uc && uc <= 0x1f77f) {
                //                    returnValue = YES;
                //                }
                if (0xdc00 <= ls && ls <= 0xe290) {
                    returnValue = YES;
                }
            }
        }
        //        else if (substring.length > 1) {
        //            const unichar ls = [substring characterAtIndex:1];
        //            if (ls == 0x20e3) {
        //                returnValue = YES;
        //            }
        //        }
        else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff) {
                returnValue = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                returnValue = YES;
            }
        }
    }];
    return returnValue;
}



- (CGSize)getRectSizeWithSize:(CGSize)size font:(UIFont *)font{
    NSDictionary *attribute = @{NSFontAttributeName: font};
    CGSize retSize = [self boundingRectWithSize:size
                                        options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                     attributes:attribute
                                        context:nil].size;
    return retSize;
}

- (CGFloat)getStringHeightWithWidth:(CGFloat)widthSize font:(UIFont *)font lineSpace:(CGFloat)lineSpace{
    CGSize size = [self getRectSizeWithSize:CGSizeMake(widthSize, MAXFLOAT) font:font];
    CGFloat stringHeight = (ceilf(size.height/[font pointSize]*1.19)-1)*lineSpace+size.height;
    return stringHeight;
}


- (CGFloat)getWidthWithFont:(UIFont *)font{
    return [self getWidthWithTitle:self font:font];
}
- (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = font;
    [label sizeToFit];
    return label.frame.size.width;
}

- (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}
- (NSMutableAttributedString *)stringToNSMutableAttributedString:(CGFloat)lineSpace{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self length])];
    return attributedString;
}

//字符串不合法
- (BOOL)stringInvalid
{
    NSString *oneStr =[NSString stringWithFormat:@"%@",self];
    BOOL r0 =(oneStr==nil);
    BOOL r1 =![oneStr length];
    if (r0||r1) {
        return YES;
    }
    BOOL r2 =([oneStr isEqualToString:@"null"]);
    BOOL r3 =([oneStr isEqualToString:@"NULL"]);
    if (r2||r3) {
        return YES;
    }
    BOOL r5 =([oneStr isEqualToString:@"(null)"]);
    BOOL r6 =([oneStr isEqualToString:@"<null>"]);
    if (r5||r6) {
        return YES;
    }
    return NO;
}
-(NSString *)getPicuRLWithSize:(NSString *)size{
    if (size==nil) {
        size=@"";
    }
    NSString *string;
    NSRange range=[self rangeOfString:@"image.myqcloud.com"];
    if (range.length>0) {
        string=[NSString stringWithFormat:@"%@/%@",self,size];
    }else{
        string=self;
    }
    //    NSLog(@"👍👍👍👍👍👍👍👍%@",string);
    return string;
}

// 此方法随机产生32位字符串
+(NSString *)ret32bitString
{
    char data[32];
    for (int x=0;x<32;data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:32 encoding:NSUTF8StringEncoding];
}
//手机号码的有效性判断
//检测是否是手机号码
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    //NSString * MOBILE = @"^1[3-9]\\d{9}$";
    NSString * MOBILE = @"^((13[0-9])|14[579]|(15[^4,\\D])|(17[^249,\\D])|(18[0-9]))\\d{8}$";

    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];

    if ([regextestmobile evaluateWithObject:mobileNum] == YES)
        {
        return YES;
        }
    else
        {
        return NO;
        }
}
//邮箱的有效性判断
-(BOOL)isValidateEmail:(NSString *)email
{
    //NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *emailRegex = @"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];

    return [emailTest evaluateWithObject:email];
    
}

//设置label上显示不同字体
- (NSMutableAttributedString *)setFontWithStr:(NSString *)str font:(int)font range:(NSRange)range
{
    //font   该字符的字号
    NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:str];
    //该字符串的位置
    [noteString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:range];
    return noteString;
}

//设置label上显示不同颜色
- (NSMutableAttributedString *)setColorWithStr:(NSString *)str range:(NSRange)range color:(UIColor *)color
{
    //label  需要操作的Label
    NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:str];
    //需要设置的位置  颜色
    [noteString addAttribute:NSForegroundColorAttributeName value:color range:range];
    return noteString;
}
@end
